package com.zy;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.ParsedSingleValueNumericMetricsAggregation;
import org.elasticsearch.search.aggregations.metrics.sum.ParsedSum;
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.Arrays;

/**
 * @author zhaoyi
 * @date 2021/2/26 17:00
 */
public class EsTs {
    public static void main(String[] args) throws IOException {

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("10.0.10.78", 9200, "http"),
                        new HttpHost("10.0.10.79", 9200, "http")));



        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        SumAggregationBuilder sum = AggregationBuilders.sum("sum_price").field("opus_num");
        searchSourceBuilder.aggregation(sum);

        SearchRequest searchRequest = new SearchRequest("app.kol_video").types("_doc");
        searchRequest.source(searchSourceBuilder);
        searchSourceBuilder.size(0);


        SearchResponse search = client.search(searchRequest);
        ParsedSingleValueNumericMetricsAggregation sumP = search.getAggregations().get("sum_price");
        System.out.println(sumP.value());



        client.close();
    }
}
